﻿#include "usemysql.h"
#include <QDebug>
#include <QMessageBox>
#include <QSqlError>

Q_GLOBAL_STATIC(UseMySQL,theInstance)

UseMySQL::UseMySQL()
{
    connectMySQL();
}

UseMySQL::~UseMySQL()
{

}

UseMySQL *UseMySQL::instance()
{
    return theInstance();
}

bool UseMySQL::connectMySQL()
{
    qDebug() << QSqlDatabase::drivers();
    base=QSqlDatabase::addDatabase("QMYSQL","Main");
    base.setDatabaseName("qtqq_main");
    base.setHostName("localHost");
    base.setUserName("root");
    base.setPassword("123456");
    base.setPort(3306);
    if(base.open())
    {
        QMessageBox::information(nullptr, "Tip", "Successed to open database", QMessageBox::Yes);
        return true;
    }
    else
    {
        QMessageBox::information(nullptr, "Tip", "Failed to open database", QMessageBox::Cancel);
        qDebug() << base.lastError().text();
        return false;
    }
}
